//
//  ListScreenViewController.m
//  TheRoadAD
//
//  Created by 宁宁 on 16/9/8.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "ListScreenViewController.h"
#import "ListScreenTableViewCell.h"//cell
#import "OthersHomePageViewController.h"//他人主页Controller


@interface ListScreenViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation ListScreenViewController
{
    NSMutableArray *_isSelectedArray;
    
    NSMutableArray *_chooseArray;
    
    BOOL _isChooseAll;
}

 static NSString *const ListScreenTableViewCellID = @"ListScreenTableViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _isSelectedArray = [NSMutableArray array];
    _chooseArray = [NSMutableArray array];
    for (int i = 0; i < 15; i++) {
        UIImageView *image = [[UIImageView alloc] init];
        [image setImage:[UIImage imageNamed:@"icon_422"]];
        NSString *string = @"NO";
        [_chooseArray addObject:@[image, string]];
    }
    
    [self registerTableView];
    
    [self setNav];
}

#pragma mark === 注册TableView ===
- (void)registerTableView{
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, 115)];
    
    UIButton *pass = [UIButton buttonWithType:UIButtonTypeCustom];
    pass.frame = CGRectMake(20, 50, Screen_Width - 40, 50);
    [pass setTitle:@"通过" forState:UIControlStateNormal];
    pass.titleLabel.font = [UIFont fontWithName:AppFont size:18];
    [pass setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    pass.backgroundColor = UIColorFromRGB(0x03dace);
    [pass addTarget:self action:@selector(passSelected) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:pass];
    
    self.tableView.tableFooterView = footerView;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ListScreenTableViewCell" bundle:nil] forCellReuseIdentifier:ListScreenTableViewCellID];
}

#pragma mark === 设置导航栏 ===
- (void)setNav{
    UIBarButtonItem *chooseAllBtn = [[UIBarButtonItem alloc] initWithTitle:@"全选" style:UIBarButtonItemStylePlain target:self action:@selector(chooseAll)];
    chooseAllBtn.tintColor = UIColorFromRGB(0x03dace);
    self.navigationItem.rightBarButtonItem = chooseAllBtn;
}

#pragma mark === TableViewDelegate ===
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 15;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 61;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ListScreenTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ListScreenTableViewCellID];
    if (_isChooseAll) {
        [cell.chooseImg setImage:[UIImage imageNamed:@"icon_522"]];
        cell.isSelected = @"YES";
    }
    else{
        cell.isSelected = _chooseArray[indexPath.row][1];
    }
    
    if ([cell.isSelected isEqualToString:@"YES"]) {
        [cell.chooseImg setImage:[UIImage imageNamed:@"icon_522"]];
    }else{
        [cell.chooseImg setImage:[UIImage imageNamed:@"icon_422"]];
    }
    
    [_chooseArray replaceObjectAtIndex:indexPath.row withObject:@[cell.chooseImg, cell.isSelected]];
    
    __weak __typeof__ (ListScreenTableViewCell *)weakCell = cell;
    cell.cellClick = ^(int temp){
        _isChooseAll = NO;
        switch (temp) {
            case 0:
                [_isSelectedArray addObject:[NSString stringWithFormat:@"%ld", indexPath.row]];
                NSLog(@"%@", _isSelectedArray);
                break;
            case 1:
                [_isSelectedArray removeObject:[NSString stringWithFormat:@"%ld", indexPath.row]];
                NSLog(@"%@", _isSelectedArray);
                break;
            case 2:
                NSLog(@"通过");
//                [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
                break;
            case 3:
            {
                OthersHomePageViewController *others = [[OthersHomePageViewController alloc] init];
                others.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:others animated:YES];
            }
                break;
            default:
                break;
        }
        [_chooseArray replaceObjectAtIndex:indexPath.row withObject:@[weakCell.chooseImg, weakCell.isSelected]];
    };
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark === 通过 ===
- (void)passSelected{
    NSLog(@"通过");
}

#pragma mark === 全选 ===
- (void)chooseAll{
    _isChooseAll = YES;
    [_isSelectedArray removeAllObjects];
    for (int i = 0; i < _chooseArray.count; i++) {
        UIImageView *image = _chooseArray[i][0];
        [image setImage:[UIImage imageNamed:@"icon_522"]];
        NSString *string = _chooseArray[i][1];
        string = @"YES";
    }
    for (int i = 0; i < 15; i++) {
        [_isSelectedArray addObject:[NSString stringWithFormat:@"%d", i]];
    }
    [_tableView reloadData];
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView= [[UITableView alloc]initWithFrame:CGRectMake(0,0, Screen_Width, Screen_Height) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;//去掉滑动竖条
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

@end
